*********************************
*SETUP CODES
*********************************
clear

log using analysis_main.log 

***
*Directory 
***
*cd to directory of choice 
use "subsidy_leader.dta"

*** 
*Analysis 
***
xtset firmID year  

*set up fe
egen industry_name_CSRC_f=group(industry_name_CSRC)
egen industry_year=group(industry_name_CSRC year)
encode current_gvn, gen(current_gvn_f)
encode current_psec, gen(current_psec_f)
encode mayor, gen(mayor_f)
encode msec, gen(msec_f)

count //1951

*Table 1: Descriptive statistics
gen subsidy_assets_number= subsidy_assets/100 //the original measure includes only the percentage number
eststo clear
estpost summarize subsidy TotalAssets subsidy_assets_number revenue ROA if subsidy!=. & private!=. 
esttab using Table1a.tex, replace ///
    cells("count(fmt(0)) mean(fmt(%15.2f)) sd(fmt(%15.2f)) min(fmt(%15.2f)) max(fmt(%15.2f))") ///
    title("Descriptive Statistics") nomtitle nonumber noobs  

tabstat subsidy, by(private) statistics(n mean sd min max) format(%9.2f) 
tabstat TotalAssets if subsidy!=., by(private) statistics(n mean sd min max) format(%15.2gc)
tabstat subsidy_assets_number if subsidy!=., by(private) statistics(n mean sd min max) format(%9.4f)
tabstat revenue if subsidy!=.,  by(private) statistics(n mean sd min max) format(%10.2gc)
tabstat ROA if subsidy!=.,  by(private) statistics(n mean sd min max) format(%9.2f)

preserve
duplicates drop gvn_turnover province year, force
tabstat gvn_turnover psec_turnover,  statistics(n mean sd min max) format(%9.2f)
restore

***********************
*Main Results 
***********************
*Table 2: Main effects
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_revenue_w, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass5

outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass5] using Table2.tex, replace label dec(3)

*Figure 1: The turnover effect
quietly reghdfe subsidy_assets_w i.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
estimates store gvn_lag0

forval x=1/5{
quietly reghdfe subsidy_assets_w  i.L`x'.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
estimates store gvn_lag`x' 
}

forval x=1/2{
quietly reghdfe subsidy_assets_w  i.F`x'.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
estimates store gvn_forward`x' 
}

outreg2 [gvn_forward2 gvn_forward1 gvn_lag0 gvn_lag1 gvn_lag2 gvn_lag3 gvn_lag4 gvn_lag5] using TableA8.tex, replace label dec(3)

coefplot gvn_forward2 gvn_forward1 gvn_lag0 gvn_lag1 gvn_lag2 gvn_lag3 gvn_lag4 gvn_lag5, /// 
    keep(1F2.gvn_turnover#1.private 1F.gvn_turnover#1.private 1.gvn_turnover#1.private 1L.gvn_turnover#1.private 1L2.gvn_turnover#1.private ///
	1L3.gvn_turnover#1.private 1L4.gvn_turnover#1.private 1L5.gvn_turnover#1.private) /// 
     yline(0, lp(dash))  vertical ///
	coeflabels(1F2.gvn_turnover#1.private="-2" 1F.gvn_turnover#1.private="-1" 1.gvn_turnover#1.private = "0" 1L.gvn_turnover#1.private="+1" ///
	1L2.gvn_turnover#1.private="+2" 1L3.gvn_turnover#1.private="+3" ///
	1L4.gvn_turnover#1.private="+4" 1L5.gvn_turnover#1.private="+5") /// 
	xtitle("Years after governor turnover", size(small)) ///
	levels(95 90) legend(order(1 "95%" 2 "90%") rows(1) position(6))  ciopts(lwidth(*1 *2) lcolor(gray black)) msymbol(C) ///
	scheme(lean1)  

graph export Figure1.png, replace 

*Table 3 - Subsidy distribution: year 0-2 and 3 years or more after turnover
preserve
keep if L.gvn_turnover==1|L2.gvn_turnover==1|gvn_turnover==1|F.gvn_turnover==1 //observations from the year before turnover are kept because independent variables are lagged
reghdfe subsidy_assets_w L.c.ROA_w##i.private L.log_assets_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto year012
outreg2 year012 using Table3.tex, replace  dec(3)
restore

preserve
drop if gvn_turnover==1 
drop if L.gvn_turnover==1
//drop if L2.gvn_turnover==1 //observations from two years after turnover are kept because independent variables are lagged
reghdfe subsidy_assets_w L.c.ROA_w##i.private L.log_assets_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto year345
outreg2 year345 using Table3.tex, append  dec(3)
restore

***********************
*Appendix
***********************
*Figure A.1 - Total subsidies by year 
graph bar (sum) subsidy, over(year) ytitle("Total subsidies") ///
plotregion(fcolor(white)) graphregion(fcolor(white)) ytitle("Total subsidies (10,000 rmb)") 

graph export FigureA1.png, replace 

*Table A.2 - Number of firms by industry  
preserve
keep if year==2015
tabout industry_name_CSRC private using TableA2.tex, style(tex) format(0) replace
restore
count if private!=. //1845
/* note: industries in Chinese:
construction: 建筑业
Hydro, environment, and public facility mangagement: 水利、环境和公共设施管理业
Electricity, thermo and water production and supply: 电力、热力、燃气及水生产和供应业
*/

*Figure A.4 
preserve
keep province current_gvn gvn_tenure
duplicates drop
unique current_gvn
tab gvn_tenure 
su gvn_tenure, detail
hist gvn_tenure, discrete xtitle("Governor tenure (year)") xlabel(0(2)10) fcolor(ltblue) graphregion(color(white)) bgcolor(white)  
graph export FigureA4.png, replace 
restore

*Table A.3 - nonwinsorized results
reghdfe subsidy_assets i.L.gvn_turnover##i.private, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets i.L.gvn_turnover##i.private L.log_revenue, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets i.L.gvn_turnover##i.private L.log_assets, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets i.L.gvn_turnover##i.private L.log_assets L.ROA, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets i.L.gvn_turnover##i.private L.log_assets L.ROA, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass5

*unique current_gvn //75 
outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass5] using TableA3.tex, replace label dec(3)

*Table A.4 - using log subsidy as Dv
su log_subsidy_w
reghdfe log_subsidy_w i.L.gvn_turnover##i.private, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_logsub1
reghdfe log_subsidy_w i.L.gvn_turnover##i.private L.log_revenue_w, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_logsub2
reghdfe log_subsidy_w i.L.gvn_turnover##i.private L.log_assets_w, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_logsub3
reghdfe log_subsidy_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_logsub4
reghdfe log_subsidy_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_logsub6

outreg2 [gvn_lag1_logsub1 gvn_lag1_logsub2 gvn_lag1_logsub3 gvn_lag1_logsub4 gvn_lag1_logsub6] using TableA4.tex, replace label dec(3)


*Table A.5 - additional firm characteristics: tax rate 
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.tax_rate_income, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.tax_rate_income, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass5

outreg2 [gvn_lag1_subass4 gvn_lag1_subass5] using TableA5.tex, replace label dec(3)


*Table A.6 - additional firm characteristics
** adding other firm level indicators 
su staff //1701 obs
gen lstaff=log(staff)
su 成立year 
gen firm_age=year-成立year 
su firm_age

reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.lstaff L.firm_age, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_revenue_w L.lstaff L.firm_age, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.lstaff L.firm_age, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w L.lstaff L.firm_age, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w L.lstaff L.firm_age, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass5

outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass5] using TableA6.tex, replace label dec(3)


*Table A.7 - effect of firm subsidies on governor turnover
reghdfe gvn_turnover c.L.subsidy_assets_w##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto m1

outreg2 m1 using TableA7.tex, replace dec(3)


*Table A.8 - Tables underlying figure 1
*codes in Figure 1 above


*Table A.9 - Analysis by governor tenure length
*Subset to only governors who have tenure periods longer than 2 years
preserve
tab gvn_tenure
keep if gvn_tenure>2
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_revenue_w, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w, absorb(year provid industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass5
outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass5] using TableA9.tex, replace label dec(3)
restore


*Figure A.5 - sensitivity analysis
gen treatment=L.gvn_turnover*private
sensemakr subsidy_assets_w i.L.gvn_turnover i.private treatment L.log_assets_w L.ROA_w ///
i.current_gvn_f i.provid i.industry_year, ///
treat(treatment) benchmark(L.log_assets_w) contourplot kd(2 4 6 8 10) 

graph export FigureA5.png, replace 


*Table A.11 - results for provincial party secretary/mayor/city party secretary 
reghdfe subsidy_assets_w i.L.psec_turnover##i.private L.log_assets_w L.ROA_w, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets_w i.L.psec_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_psec_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets_w i.L.mayor_turnover##i.private L.log_assets_w L.ROA_w, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets_w i.L.mayor_turnover##i.private L.log_assets_w L.ROA_w, absorb(mayor_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.msec_turnover##i.private L.log_assets_w L.ROA_w, absorb(provid industry_year) vce(cluster provid)
est sto gvn_lag1_subass5
reghdfe subsidy_assets_w i.L.msec_turnover##i.private L.log_assets_w L.ROA_w, absorb(msec_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass6

outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass5 gvn_lag1_subass6] using TableA11.tex, replace label dec(3)


*Table A.14 - Triple interaction with governors near retirement
preserve
gen gvn_age_num=real(gvn_age)
drop if gvn_age=="NA"

*calculate governor starting age
egen gvn_age_max = max(gvn_age_num), by(current_gvn province)
gen gvn_age_start=gvn_age_max-gvn_tenure 
egen gvn_start=min(gvn_age_start>58), by(current_gvn province)
tab gvn_start
unique current_gvn if gvn_start==1 //10
tab province if gvn_start==1 

*interaction model 
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private##i.gvn_start, absorb(year industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private##i.gvn_start L.log_revenue_w, absorb(year industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private##i.gvn_start L.log_assets_w, absorb(year industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private##i.gvn_start L.log_assets_w L.ROA_w, absorb(industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private##i.gvn_start L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass5

outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass5] using TableA14.tex, replace label dec(3)
restore
 
*Fig A.6: distribution graphs on ROA for private and SOEs
preserve
collapse (mean) ROA_w, by(private industry_name_CSRC) 
drop if private==.
reshape wide ROA_w, i(industry) j(private)
rename ROA_w0 ROA_w_SOE 
rename ROA_w1 ROA_w_private
graph bar ROA_w_private ROA_w_SOE, over(industry_name_CSRC, relabel(1 "Construction" 2 "Hydro, environment" 3 "Electricity, thermo, gas")) ///
legend(label(1 "Private enterprises") label (2 "State-owned enterprises")) ///
bgcolor(white) graphregion(color(white)) nofill ///
bar(1, color(black%50)) bar(2, color(black%30))
*title("Average ROA for private firms and SOEs across industries")
*note("") 
graph export "FigureA6.png", as(png) replace
restore


*Table A.15 - omparison of profit levels for private firms and SOEs
preserve
keep if L.gvn_turnover==1|L2.gvn_turnover==1|gvn_turnover==1|F.gvn_turnover==1
reghdfe ROA_w i.private L.log_assets_w, absorb(provid industry_name_CSRC_f year) vce(cluster provid)
est sto est12
outreg2 est12 using TableA15.tex, replace label dec(3)
restore

preserve
drop if gvn_turnover==1 
drop if L.gvn_turnover==1
reghdfe ROA_w i.private L.log_assets_w, absorb(provid industry_name_CSRC_f year) vce(cluster provid)
est sto est345
outreg2 year345 using TableA15.tex, append  dec(3)
restore

*full sample
reghdfe ROA_w i.private L.log_assets_w, absorb(provid industry_name_CSRC_f year) vce(cluster provid) 
est sto est3
outreg2 est3 using TableA15.tex, append label dec(3)

log close
